package com.leyou.server;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leyou.common.exception.pojo.ExceptionEnum;
import com.leyou.common.exception.pojo.LyException;
import com.leyou.common.pojo.PageResult;
import com.leyou.item.pojo.Brand;
import com.leyou.mapper.BrandMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BrandService {
    @Autowired(required = false)
    private BrandMapper brandMapper;
    public PageResult<Brand> brandPageQuery(Integer page, Integer rows, String sortBy, Boolean desc, String key) {
        //1.封装查询条件
        //1.1 IPage对象：封装分页参数
        //(查询前：IPage对象封装页面->后端数据)
        IPage<Brand> ipage = new Page(page, rows);
        //2.查询数据，获取结果
        QueryWrapper<Brand> wrapper = Wrappers.query();
          if (StringUtils.isNotEmpty(key)){
              wrapper.like("name",key).or().eq("letter",key.toUpperCase());
          }
          if (StringUtils.isNotEmpty(sortBy)){
              if (desc){
                  wrapper.orderByDesc(sortBy);
              }else{
                wrapper.orderByDesc(sortBy);
              }
          }
        //3.处理(封装)结果
        ipage = brandMapper.selectPage(ipage, wrapper);
        //4.返回结果
        PageResult<Brand> pageResult = new PageResult<>(ipage.getTotal(), ipage.getPages(), ipage.getRecords());
        return pageResult;
    }

    public void brandSave(List<Long> cids, Brand brand) {
        try {
            //1.保存品牌表数据
            brandMapper.insert(brand);
            //2.保存分类品牌表数据
            brandMapper.saveCategoryAndBrand(brand.getId(),cids);
        } catch (Exception e) {
            e.printStackTrace();
         throw  new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    public Brand findBrandById(Long id) {

        Brand brand = brandMapper.selectById(id);

        if (brand==null){
            throw  new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return brand;
    }

    public List<Brand> findBrandsByCid(Long id) {
        List<Brand> brands = brandMapper.findBrandsByCid(id);
        if (CollectionUtils.isEmpty(brands)){
            throw  new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return brands;
    }

    public List<Brand> findBrandsByIds(List<Long> ids) {
        List<Brand> brands = brandMapper.selectBatchIds(ids);
        if(CollectionUtils.isEmpty(brands)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return brands;
    }
}
